package top.frsf.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 配置文件读取工具
 *
 * @author FRSF
 * @since 2025/8/1 9:52
 */
@Slf4j
public class ConfigLoader {

    private static final Properties properties = new Properties();

    static {
        // 读取配置文件
        try (FileInputStream input = new FileInputStream("src/main/resources/config.properties")) {
            properties.load(input);
        } catch (IOException e) {
            log.error("读取配置文件失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取对应key的配置值(string)
     *
     * @param key 配置键
     * @return 配置值
     */
    public static String getString(String key) {
        return properties.getProperty(key);
    }

    /**
     * 获取对应key的配置值(int)
     *
     * @param key 配置键
     * @return 配置值
     */
    public static int getInt(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            throw new RuntimeException("配置项 " + key + " 不存在");
        }

        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            log.error("配置项 {} 的值不是有效整数", key);
            throw new RuntimeException(e);
        }
    }

}
